package com.wx.activitys.drp.timer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.wx.activitys.drp.Utils.DateUtil;
import com.wx.activitys.drp.mapper.BUserIncomeMapper;
import com.wx.activitys.drp.mapper.UserBusinessMapper;
import com.wx.activitys.drp.mapper.UserInfoMapper;
import com.wx.activitys.drp.model.BUserBusiness;
import com.wx.activitys.drp.model.BUserIncome;

@Component("taskJob")
public class TaskJob {
	@Autowired
	UserBusinessMapper userBusinessMapper;
	@Autowired
	UserInfoMapper userInfoMapper;
	@Autowired
	BUserIncomeMapper bUserIncomeMapper;

	/**
	 * 每月循环任务定时器（1点）
	 */
/*	@Scheduled(cron = "0 0 1 1 * ?")
	public void monthTask() {
		String dateNowStr = DateUtil.getLastDate();
		Map<String, Object> param = new HashMap<>();
		param.put("month", dateNowStr);
		param.put("roleKey", "4");
		List<BUserBusiness> lists = userBusinessMapper.listByMap(param);
		for (BUserBusiness bUserBusiness : lists) {
			if (bUserBusiness.getVolume() < 100) {
				param.clear();
				param.put("openid", bUserBusiness.getOpenid());
				param.put("roleKey", "6");
				userInfoMapper.update(param);
			}
		}
	}*/
	
	
	/**
	 * 每月1号凌晨计算上个月平台会员分销所得的利润(0点10分)
	 */
	@Scheduled(cron = "0 10 0 1 * ?")
	public void monthCountTask() {
		String startDate=DateUtil.getBeforeFirstMonthdate();
		String endDate = DateUtil.getBeforeLastMonthdate();
		Map<String, Object> param = new HashMap<>();
		param.put("startDate", startDate);
		param.put("endDate", endDate);
		param.put("type", "distribution");
		List<BUserIncome> lists = bUserIncomeMapper.listIncome(param);
		for (BUserIncome bUserIncome : lists) {
			param.clear();
			param.put("openid", bUserIncome.getOpenid());
			param.put("incomeadd", bUserIncome.getIncome());
			userInfoMapper.update(param);
		}
	}

	/**
	 * 每天循环任务定时器(23点)
	 */
/*	@Scheduled(cron = "0 0 23 * * ?")
	public void dayTask() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String dateNowStr = sdf.format(new Date());
		Map<String, Object> param = new HashMap<>();
		param.put("month", dateNowStr);
		param.put("roleKey", "6");
		List<BUserBusiness> lists = userBusinessMapper.listByMap(param);
		for (BUserBusiness bUserBusiness : lists) {
			if (bUserBusiness.getVolume() > 100) {
				param.clear();
				param.put("openid", bUserBusiness.getOpenid());
				param.put("roleKey", "4");
				userInfoMapper.update(param);
			}
		}
	}*/
}
